package com.prs.crm.action.crm;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import com.prs.crm.domain.crm.CustomerSource;
import com.prs.crm.service.crm.CustomerSourceService;

public class CustomerSourceAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private PaginationSupport pagination;
	private CustomerSource customerSource;
	private CustomerSourceService customerSourceService;
	private Integer[] sourceIds;
	
	@Action("listCustomerSource")
	public String listCustomerSource(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CustomerSource.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.customerSourceService.listByPage(
				criteria, pagination));
		return SUCCESS;
	}
	@Action("editCustomerSource")
	public String editCustomerSource(){
		if(this.sourceIds!=null){
			this.customerSource=this.customerSourceService.get(sourceIds[0]);
		}
		return SUCCESS;
	}
	@Action(value = "savebtmSource", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerSource"),
			@Result(name = "error", type = "chain", location = "listCustomerSource")})
	public String savebtmSource(){
		this.customerSource=this.customerSourceService.get(sourceIds[0]);
		customerSource.setRemark("默认");
		this.customerSourceService.submit(customerSource);
		return SUCCESS;
	}
	@Action(value = "savebtqSource", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerSource"),
			@Result(name = "error", type = "chain", location = "listCustomerSource")})
	public String savebtqSource(){
		List<CustomerSource> list=this.customerSourceService.getAllSources();
		for(int i=0;i<list.size();i++){
			CustomerSource s=list.get(i);
			if(s.getRemark()!=null){
				s.setRemark("");
				this.customerSourceService.submit(s);
			}
		}
		return SUCCESS;
	}
	@Action(value = "saveCustomerSource", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerSource"),
			@Result(name = "error", type = "chain", location = "editCustomerSource")})
	public String saveCustomerAddress(){
		if(this.customerSource.getId()==null){
			this.customerSourceService.save(customerSource);
		}else{
			this.customerSourceService.submit(customerSource);
		}
		return SUCCESS;
	}
	@Action(value = "removeCustomerSource", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerSource"),
			@Result(name = "error", type = "chain", location = "listCustomerSource")})
	public String removeCustomerSource(){
		this.customerSourceService.batchRemove(sourceIds);
		return SUCCESS;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public CustomerSource getCustomerSource() {
		return customerSource;
	}
	public void setCustomerSource(CustomerSource customerSource) {
		this.customerSource = customerSource;
	}
	public CustomerSourceService getCustomerSourceService() {
		return customerSourceService;
	}
	public void setCustomerSourceService(CustomerSourceService customerSourceService) {
		this.customerSourceService = customerSourceService;
	}
	public Integer[] getSourceIds() {
		return sourceIds;
	}
	public void setSourceIds(Integer[] sourceIds) {
		this.sourceIds = sourceIds;
	}
	
	
}
